package com.xzxy.xmlg_backend.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.xzxy.xmlg_backend.entity.Result;
import com.xzxy.xmlg_backend.service.IFileService;
import com.xzxy.xmlg_backend.utils.AliOSSProperties;
import com.xzxy.xmlg_backend.utils.FileUploadUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;


/**
 * @Author: zys
 * @CreateTime: 2025-06-12
 */

@RestController
@RequestMapping("/file")
@Tag(name = "文件管理")
public class FileUploadController {

    @Autowired
    private FileUploadUtils fileUploadUtils;


    @Autowired
    private AliOSSProperties aliOSSProperties;

    @Autowired
    private IFileService fileService;

    @PostMapping
    @Operation(summary = "阿里云文件上传", description = "阿里云文件上传")
    public Result<String> AlyOSS(@RequestParam("image") MultipartFile image) {
        String fileName = image.getOriginalFilename();

        try {
            OSS ossClient = new OSSClientBuilder().build(
                    aliOSSProperties.getEndpoint(),
                    aliOSSProperties.getAccessKeyId(),
                    aliOSSProperties.getAccessKeySecret()
            );

            // 上传文件
            ossClient.putObject(
                    aliOSSProperties.getBucketName(),
                    fileName,
                    new ByteArrayInputStream(image.getBytes())
            );

            // 关闭客户端
            ossClient.shutdown();

            // 拼接外网访问地址
            String url = "https://" + aliOSSProperties.getBucketName() + "."
                    + aliOSSProperties.getEndpoint() + "/" + fileName;

            return Result.success(url); // 返回 URL
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 头像上传
     *
     * @param file
     * @return
     */
    @PostMapping("/upload/avatar")
    @Operation(summary = "头像上传", description = "头像上传")
    public Result uploadAvatar(@RequestParam("file") MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName = "avatar/" + java.util.UUID.randomUUID().toString() + fileExtension;

        try {
            OSS ossClient = new OSSClientBuilder().build(
                    aliOSSProperties.getEndpoint(),
                    aliOSSProperties.getAccessKeyId(),
                    aliOSSProperties.getAccessKeySecret()
            );

            ossClient.putObject(
                    aliOSSProperties.getBucketName(),
                    fileName,
                    new ByteArrayInputStream(file.getBytes())
            );

            ossClient.shutdown();

            String url = "https://" + aliOSSProperties.getBucketName() + "." + aliOSSProperties.getEndpoint() + "/" + fileName;

            return Result.success(url);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 班级导入
     *
     * @param file
     * @return
     */
    @PostMapping("/upload/class")
    @Operation(summary = "班级导入", description = "班级导入")
    public Result uploadClass(@RequestParam("file") MultipartFile file) {
        System.out.println("正在导入文件(班级)......");
        return fileUploadUtils.uploadClass(file);
    }

    /**
     * 员工导入
     *
     * @param file
     * @return
     */
    @PostMapping("/upload/emp")
    @Operation(summary = "员工导入", description = "员工导入")
    public Result uploadEmp(@RequestParam("file") MultipartFile file) {
        System.out.println("正在导入文件(员工)......");
        return fileUploadUtils.uploadEmp(file);
    }

    /**
     * 学员导入
     *
     * @param file
     * @return
     */
    @PostMapping("/upload/student")
    @Operation(summary = "学员导入", description = "学员导入")
    public Result uploadStudent(@RequestParam("file") MultipartFile file) {
        System.out.println("正在导入文件(学员)......");
        return fileUploadUtils.uploadStudent(file);
    }


    /**
     * 下载班级模板
     *
     * @param response
     */
    @Operation(summary = "下载班级模板", description = "下载班级模板")
    @GetMapping(value = "/downloadClass", consumes = MediaType.ALL_VALUE)
    public void downloadClassTemplate(HttpServletResponse response) {
        fileService.downloadClassTemplate(response);
    }

    /**
     * 下载员工模板
     *
     * @param response
     */
    @Operation(summary = "下载员工模板", description = "下载员工模板")
    @GetMapping(value = "/downloadEmp", consumes = MediaType.ALL_VALUE)
    public void downloadEmpTemplate(HttpServletResponse response) {
        fileService.downloadEmpTemplate(response);
    }

    /**
     * 下载学员模板
     *
     * @param response
     */
    @Operation(summary = "下载学员模板", description = "下载学员模板")
    @GetMapping(value = "/downloadStudent", consumes = MediaType.ALL_VALUE)
    public void downloadStudentTemplate(HttpServletResponse response) {
        fileService.downloadStudentTemplate(response);
    }

    /**
     * 班级数据导出
     *
     * @param response
     */
    @Operation(summary = "班级数据导出", description = "班级数据导出")
    @GetMapping(value = "/exportExcelClass", consumes = MediaType.ALL_VALUE)
    public void exportExcelClass(HttpServletResponse response) {
        fileService.exportExcelClass(response);
    }

    /**
     * 员工数据导出
     *
     * @param response
     */
    @Operation(summary = "员工数据导出", description = "员工数据导出")
    @GetMapping(value = "/exportExcelEmp", consumes = MediaType.ALL_VALUE)
    public void exportExcelEmp(HttpServletResponse response) {
        fileService.exportExcelEmp(response);
    }

    /**
     * 学员数据导出
     *
     * @param response
     */
    @Operation(summary = "学员数据导出", description = "学员数据导出")
    @GetMapping(value = "/exportExcelStudent", consumes = MediaType.ALL_VALUE)
    public void exportExcelStudent(HttpServletResponse response) {
        fileService.exportExcelStudent(response);
    }
}
